package com.bole.controller;


import com.alibaba.fastjson.JSONObject;
import com.bole.constant.ContentRedis;
import com.bole.dao.CarDao;
import com.bole.dao.ContentDao;
import com.bole.dto.car.TbCarDto;
import com.bole.dto.content.TbContentDto;
import com.bole.pojo.BrowsingHistoryDo;
import com.bole.pojo.TbCarDo;
import com.bole.pojo.TbContentDo;
import com.bole.service.BrowsingHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import sun.plugin.services.BrowserService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("content")
@Api(value = "广告管理api")
public class ContentController {

    @Autowired
    private ContentDao contentDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CarDao carDao;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    //查询轮播图
    @PostMapping("selectRotation")
    @ApiImplicitParam(name = "contentTypeId", value = "关联类型ID", required = true, dataTypeClass = Long.class)
    public List<TbContentDto> selectRotation(@RequestParam Long contentTypeId){
        //通过contentTypeId查询redis里是否存在
        if(redisTemplate.hasKey(ContentRedis.CONTENT_ID_KEY + contentTypeId)){
            //如果存在直接取redis
            String s = (String) redisTemplate.boundValueOps(ContentRedis.CONTENT_ID_KEY + contentTypeId).get();
            List<TbContentDto> tbContents = JSONObject.parseArray(s, TbContentDto.class);
            return tbContents;
        }else{
            //如果redis里不存在，查询数据库
            List<TbContentDo> contentDaoList = contentDao.findAllByContentTypeId(contentTypeId);
            ArrayList<TbContentDto> arrayList = new ArrayList<>();
            for (TbContentDo TbContentDo : contentDaoList) {
                TbContentDto tbContentDto = new TbContentDto();
                BeanUtils.copyProperties(TbContentDo,tbContentDto);
                arrayList.add(tbContentDto);
            }
            //转换成String类型
            String s = JSONObject.toJSONString(arrayList);
            //存进redis
            redisTemplate.boundValueOps(ContentRedis.CONTENT_ID_KEY + contentTypeId).set(s);
            //设置过期时间
            redisTemplate.expire(ContentRedis.CONTENT_ID_KEY,1, TimeUnit.DAYS);
            return arrayList;
        }
    }

    //热卖好车做千人千面
    @PostMapping("selectIntelligence")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataTypeClass = Long.class)
    public List<TbCarDto> selectIntelligence(@RequestParam("userId") Long userId) {
        //通过用户的id去查询浏览记录表
       /* Query query = new Query(Criteria.where("userId").is(userId));
        List<BrowsingHistoryDo> historyDoList = mongoTemplate.find(query, BrowsingHistoryDo.class);*/
        List<BrowsingHistoryDo> historyDoList = browsingHistoryService.selectByUserId(userId);
        //List<BrowsingHistoryDo> historyDoList = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), BrowsingHistoryDo.class);
        System.out.println(historyDoList);
        //判断是否为空
        if (CollectionUtils.isEmpty(historyDoList)) {
            //如果为空，则没有浏览记录，走默认
            List<TbCarDto> carDtos = new ArrayList<>();
            //查询审核通过的前六条
            List<TbCarDo> all = carDao.findAllByStatus();
            for (TbCarDo tbCarDo : all) {
                TbCarDto tbCarDto = new TbCarDto();
                BeanUtils.copyProperties(tbCarDo,tbCarDto);
                carDtos.add(tbCarDto);
            }

            return carDtos;
        } else {
            List<TbCarDo> carDoList = new ArrayList<>();
            for (BrowsingHistoryDo browsingHistoryDo : historyDoList) {
                //拿到汽车的id
                Long carId1 = browsingHistoryDo.getCarId();
                //用carId去查car表
                TbCarDo tbCarDo = carDao.findById(carId1).get();
                //放到list集合里
                carDoList.add(tbCarDo);
            }
            //去掉相同的品牌id的车辆
            List<TbCarDo> carDos = new ArrayList<>();
            //拷贝一份
            BeanUtils.copyProperties(carDoList, carDos);
            for (int i = 0; i < carDoList.size(); i++) {
                TbCarDo c = carDoList.get(i);
                for (int i1 = 0; i1 < carDos.size(); i1++) {
                    TbCarDo tbCarDo = carDos.get(i);
                    Long brandId = c.getBrandId();
                    Long brandId1 = tbCarDo.getBrandId();
                    if (brandId.equals(brandId1)) {
                        carDos.remove(c);
                    }
                }
            }

            //通过品牌id 去查询车辆
            List<TbCarDto> carBrandList = new ArrayList<>();
            for (TbCarDo tbCarDo : carDoList) {
                Long brandId = tbCarDo.getBrandId();
                List<TbCarDo> allByBrandId = carDao.findAllByBrandId(brandId);
                for (TbCarDo carDo : allByBrandId) {
                    TbCarDto tbCarDto = new TbCarDto();
                    BeanUtils.copyProperties(carDo, tbCarDto);
                    carBrandList.add(tbCarDto);
                }
            }

            return carBrandList;
        }
    }

}
